<%+header%>
<!--
    This module is a demo to configure MTK' proprietary WiFi driver.
    Basic idea is to bypass uci and edit wireless profile (mt76xx.dat) directly.
    LuCI's WiFi configuration is more logical and elegent, but it's quite tricky to 
    translate uci into MTK's WiFi profile (like we did in "uci2dat").

    Hua Shao <nossiac@163.com>
-->

<%
local mtkwifi = require("mtkwifi")
local devs = mtkwifi.get_all_devs()
local l1dat, l1 = mtkwifi.__get_l1dat()
local dridx = l1.DEV_RINDEX
local main_ifname
local chipname
%>

    <style>
        table,td,th
        {
            border-top:1px solid ;
            border-spacing: 0;
            border-color:#E0E0E0;
        }
    </style>

    <h2><%:Wireless%></h2>

    <% if #devs == 0 then %>
        <div class="alert-message">
            <big><strong><%:No wireless device found!%></strong></big>
        </div>
    <% else %>

    <fieldset class="cbi-section">
    <table class="cbi-section-table" style="empty-cells:hide">
    <% for _,dev in ipairs(devs) do %>
        <% main_ifname = l1dat and l1dat[dridx][dev.devname].main_ifname or dbdc_prefix[mainidx][subidx].."0" %>
        <% if mtkwifi.exists("/sys/class/net/"..main_ifname) then %>
            <!-- physical device -->
            <tbody>
                <% if chipname ~= string.split(dev.devname,".")[1].."."..(dev.mainidx) then %>
                    <% chipname = string.split(dev.devname,".")[1].."."..(dev.mainidx) %>
                    <tr  style="height:45px">
                        <td style="padding:15px;">
                            <img src="/luci-static/resources/icons/wifi_big.png">
                        </td>
                        <td colspan="2" style="text-align:left">
                            <big><strong title="<%=dev.profile%>"> <%=string.split(dev.devname,".")[1]%></strong></big>
                            <%if not dev.vifs then%>
                                <span style="color:red;">* <%:FATAL ERROR: Incorrect Profile Settings%></span>
                            <%end%>
                            <br>
                            <%if dev.version then%>
                                <span> <%:Driver version%>: <%=dev.version%> <br/></span>
                            <%end%>
                        </td>
                        <td style="width:310px;text-align:right">
                            <div id="ActionBtns_<%=string.split(dev.devname,".")[1]%>">
                                <button class="cbi-button cbi-button-edit" onclick="visit_url('<%=string.split(dev.devname,".")[1]%>',
                                    '<%=luci.dispatcher.build_url("admin", "network", "wifi", "chip_cfg_view", dev.devname)%>')"><%:CONFIG%></button>
                            </div>
                            <div id="BusyText_<%=string.split(dev.devname,".")[1]%>" style="display:none">
                                <big><strong><%:Processing request.%></strong></big>
                            </div>
                        </td>
                    </tr>
                <% end %>
                <tr>
                    <td style="width:56px"></td>
                    <td colspan="2" style="text-align:left;padding-left:25px;">
                        <big><strong title="<%=dev.profile%>"><%=dev.devname%></strong></big>
                            <%local diff = mtkwifi.diff_profile(dev.profile)%>
                            <%if next(diff) ~= nil then%>
                                <span style="color:red;">* <%:need reload to apply changes%></span>
                            <%end%>
                        <br>
                        <%local cfgs = mtkwifi.load_profile(dev.profile)%>
                        <span> <%:Current Band%>: <%=mtkwifi.band(string.split(cfgs.WirelessMode,";")[1])%> <br/> </span>
                        <% if dev.Enable == "0" then%>
                        <span style="color:red;"> <%:Wireless device is disabled%> </span>
                        <%else%>
                        <span> <%:Work mode%>: <% if dev.ApCliEnable == "1" then %> APCli <% else %> AP <% end %> <br/> </span>
                        <%local temp = mtkwifi.get_temp(dev.devname)%>
                        <%if temp then%>
                            <span> <%:Tempature%>: <%=temp%>&#176;C <br/> </span>
                        <%end%>
                        <%local txpwr = mtkwifi.get_txpwr(dev.devname)%>
                        <%if txpwr then%>
                            <span> <%:Tx Power%>: <%=txpwr%> dbm <br/> </span>
                        <%end%>
                        <%end%>
                    </td>
                    <td style="width:310px;text-align:right">
                        <div id="ActionBtns_<%=dev.devname%>">
                            <%if not dev.vifs then%>
                                <button class="cbi-button cbi-button-apply" id="reset" onclick="visit_url('<%=dev.devname%>',
                                    '<%=luci.dispatcher.build_url("admin", "network", "wifi", "reset_to_defaults", dev.devname)%>')"><%:Restore Profile to Factory Settings%></button>
                            <%else%>
                                <button class="cbi-button cbi-button-reset" onclick="visit_url_confirm('<%:Are you sure to RELOAD%> <%=dev.devname%>?',
                                    '<%=dev.devname%>','<%=luci.dispatcher.build_url("admin", "network", "wifi", "reload", dev.devname)%>')"><%:RELOAD%></button>
                                <button class="cbi-button cbi-button-edit" onclick="visit_url('<%=dev.devname%>',
                                    '<%=luci.dispatcher.build_url("admin", "network", "wifi", "dev_cfg_view", dev.devname)%>')"><%:CONFIG%></button>
                                <button class="cbi-button cbi-button-add" onclick='add_vif("<%=dev.devname%>","<%=dev.vifs.__prefix%>","<%=dev.maxVif%>")'><%:ADD%></button>
                            <%end%>
                        </div>
                        <div id="BusyText_<%=dev.devname%>" style="display:none">
                            <big><strong><%:Processing request.%></strong></big>
                        </div>
                    </td>
                </tr>
                <!-- /physical device -->
                <% if dev.vifs and dev.Enable == "1" then%>
                    <!-- network list -->
                    <% for _,vif in ipairs(dev.vifs) do %>
                    <tr class="cbi-section-table-row cbi-rowstyle-1">
                        <td></td>
                        <td class="cbi-value-field" style="width:16px; padding:3px;padding-left:40px;">
                            <% if vif.state == "up" then %>
                            <img src="/luci-static/resources/icons/signal-75-100.png">
                            <% else %>
                            <img src="/luci-static/resources/icons/signal-none.png">
                            <% end %>
                        </td>
                        <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
                            <strong><%:Interface%>:</strong> <%=vif.vifname%> |
                            <strong><%:Type%>:</strong> AP |
                            <strong>SSID:</strong>
                                <span id="SSID_<%=vif.vifname%>">
                                    <% if vif.__ssid == "" then %>
                                        <span style="color:red;"><%:Error: value not present in dat file%></span>
                                    <% else %>
                                        <span> <%=vif.__ssid and vif.__ssid:gsub(" ","&nbsp;") or nil%> <% end %> </span>
                                </span> |
                            <strong><%:Channel%>:</strong>
                                <span id="CHANNEL_<%=vif.vifname%>"><%=vif.__channel or dev.Channel%></span>
                            <br>
                            <% if vif.state == "up" then %>
                            <strong>BSSID:</strong> <span id="BSSID_<%=vif.vifname%>"><%=vif.__bssid%></span> | <strong><%:Wireless Mode%>:</strong> <span><%=dev.WirelessModeList[tonumber(vif.__wirelessmode)]%></span>
                            <% else %>
                            <%:Wireless is disabled or not associated%>
                            <% end %>
                        </td>
                        <td class="cbi-value-field" style="text-align:right">
                            <div id="ActionBtns_<%=vif.vifname%>">
                                <% if not vif.state then %>
                                <button class="cbi-button cbi-button-apply" onclick="alert('Please click on Reload button of ' + '<%=dev.devname%>' + ' device to enable this interface.')"><%:ENABLE%></button>
                                <% elseif vif.state == "up" then %>
                                <button class="cbi-button cbi-button-reset" onclick="visit_url_confirm('<%:Are you sure to DISABLE%> <%=vif.vifname%>?',
                                    '<%=vif.vifname%>','<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_disable", vif.vifname)%>')"><%:DISABLE%></button>
                                <% else %>
                                <button class="cbi-button cbi-button-apply" onclick="visit_url('<%=vif.vifname%>',
                                    '<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_enable", vif.vifname)%>')"><%:ENABLE%></button>
                                <% end %>
                                <button class="cbi-button cbi-button-edit" onclick="visit_url('<%=vif.vifname%>',
                                    '<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_cfg_view", dev.devname, vif.vifname)%>')"><%:CONFIG%></button>
                                <button class="cbi-button cbi-button-remove" onclick="visit_url_confirm('<%:Are you sure to REMOVE%> <%=vif.vifname%>?',
                                    '<%=vif.vifname%>','<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_del", dev.devname, vif.vifname)%>')"><%:REMOVE%></button>
                            </div>
                            <div id="BusyText_<%=vif.vifname%>" style="display:none">
                                <big><strong><%:Processing request.%></strong></big>
                            </div>
                        </td>
                    </tr>
                    <% end %>
                    <!-- /network list -->
                    <!-- apcli list -->
                    <% if dev.apcli then %>
                    <tr>
                        <td></td>
                        <td class="cbi-value-field" style="width:16px; padding:3px;padding-left:40px;">
                            <% if dev.apcli.state == "up" then %>
                            <img src="/luci-static/resources/icons/signal-75-100.png">
                            <% else %>
                            <img src="/luci-static/resources/icons/signal-none.png">
                            <% end %>
                        </td>
                        <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px"><strong><%:Interface%>:</strong> <%=dev.apcli.devname%> | <strong><%:Type%>:</strong> STA | <strong><%:Status%>:</strong> <span id="ConnStatus_<%=dev.apcli.devname%>"><% if dev.ApCliEnable ~= "1" then %> <%:Disconnected%> <% end %></span>
                            <br>
                            <div id="LoadingBar_<%=dev.apcli.devname%>" <% if dev.ApCliEnable ~= "1" then %> style="display:none" <% end %>>
                                <img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="width:20px;height:20px;vertical-align:middle;"/> <cite>&nbsp;<%:Loading connection information of%> <%=dev.apcli.devname%></cite>
                            </div>
                            <div id="ConnInfo_<%=dev.apcli.devname%>" style="display:none">
                                <strong>BSSID:</strong> <span id="ApCliBssid_<%=dev.apcli.devname%>"></span> |
                                <strong>SSID:</strong> <span id="ApCliSsid_<%=dev.apcli.devname%>"></span>
                            </div>
                            <span id="DisConnMsg_<%=dev.apcli.devname%>" <% if dev.ApCliEnable == "1" then %> style="display:none" <% end %>><%:Wireless is disabled or not associated%></span>
                        </td>
                        <td style="text-align:right">
                            <div id="ActionBtns_<%=dev.apcli.devname%>">
                                <% if dev.ApCliEnable ~= "1" then %>
                                    <% if dev.apcli.state == "up" then %>
                                    <button class="cbi-button cbi-button-reset" onclick="visit_url_confirm('<%:Are you sure to DISABLE%> <%=dev.apcli.devname%>?',
                                        '<%=dev.apcli.devname%>','<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_disable", dev.apcli.vifname)%>')"><%:DISABLE%></button>
                                    <button class="cbi-button cbi-button-reload" onclick="visit_url_confirm('<%:Are you sure to RECONNECT%> <%=dev.apcli.devname%>?',
                                        '<%=dev.apcli.devname%>','<%=luci.dispatcher.build_url("admin", "network", "wifi", "apcli_connect", dev.devname, dev.apcli.vifname)%>')"><%:RECONNECT%></button>
                                    <% end %>
                                    <button class="cbi-button cbi-button-edit" onclick="visit_url('<%=dev.apcli.devname%>',
                                        '<%=luci.dispatcher.build_url("admin", "network", "wifi", "apcli_cfg_view", dev.devname, dev.apcli.vifname)%>')"><%:CONFIG%></button>
                                <% else %>
                                    <button class="cbi-button cbi-button-reset" style="display:none" id="ApCliDownBtn_<%=dev.apcli.devname%>" onclick="visit_url_confirm('<%:Are you sure to DISABLE%> <%=dev.apcli.devname%>?',
                                        '<%=dev.apcli.devname%>','<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_disable", dev.apcli.vifname)%>')"><%:DISABLE%></button>
                                    <button class="cbi-button cbi-button-apply" style="display:none" id="ApCliUpBtn_<%=dev.apcli.devname%>" onclick="visit_url('<%=dev.apcli.devname%>',
                                        '<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_enable", dev.apcli.vifname)%>')"><%:ENABLE%></button>
                                    <button class="cbi-button cbi-button-remove" style="display:none" id="ApCliDisConnBtn_<%=dev.apcli.devname%>" onclick="visit_url_confirm('<%:Are you sure to DISCONNECT%> <%=dev.apcli.devname%>?',
                                        '<%=dev.apcli.devname%>','<%=luci.dispatcher.build_url("admin", "network", "wifi", "apcli_disconnect", dev.devname, dev.apcli.vifname)%>')"><%:DISCONNECT%></button>
                                    <button class="cbi-button cbi-button-reload" style="display:none" id="ApCliConnBtn_<%=dev.apcli.devname%>" onclick="visit_url_confirm('<%:Are you sure to RECONNECT%> <%=dev.apcli.devname%>?',
                                        '<%=dev.apcli.devname%>','<%=luci.dispatcher.build_url("admin", "network", "wifi", "apcli_connect", dev.devname, dev.apcli.vifname)%>')"><%:RECONNECT%></button>
                                    <button class="cbi-button cbi-button-edit"  style="display:none" id="ApCliCfgBtn_<%=dev.apcli.devname%>" onclick="visit_url('<%=dev.apcli.devname%>',
                                        '<%=luci.dispatcher.build_url("admin", "network", "wifi", "apcli_cfg_view", dev.devname, dev.apcli.vifname)%>')"><%:CONFIG%></button>
                                <% end %>
                            </div>
                            <div id="BusyText_<%=dev.apcli.devname%>" style="display:none">
                                <big><strong><%:Processing request.%></strong></big>
                            </div>
                        </td>
                    </tr>
                    <% end %>
                    <!-- /apcli list -->
                <% end %>
            </tbody>
        <% end %>
    <% end %>
    </table>
    </fieldset>

    <fieldset class="cbi-section">
        <h2><%:Station List%></h2>
        <table class="cbi-section-table" style="margin: auto" id="iw-assoclist">
            <tr class="cbi-section-table-titles">
                <th class="cbi-section-table-cell"><%:Interface%></th>
                <th class="cbi-section-table-cell">SSID</th>
                <th class="cbi-section-table-cell"><%:MAC-Address%></th>
                <th class="cbi-section-table-cell"><%:Hostname%></th>
                <th class="cbi-section-table-cell"><%:Security%></th>
                <th class="cbi-section-table-cell"><%:Signal%> (dBm)</th>
                <th class="cbi-section-table-cell"><%:TX Rate%></th>
                <th class="cbi-section-table-cell"><%:RX Rate%></th>
                <th class="cbi-section-table-cell"><%:Connected Time%></th>
            </tr>
            <tr class="cbi-section-table-row cbi-rowstyle-2">
                <td class="cbi-value-field" colspan="9">
                    <em><%:Collecting data...%></em>
                </td>
            </tr>
        </table>
    </fieldset>
    <% end %>

<script>

    function clean_stalist_table()
    {
        var table = document.getElementById('iw-assoclist');
        //console.log("clean_aplist_table.aplist len=" + table.rows.length);
        while(table.rows.length > 1)
            table.deleteRow(-1);
    }

    function is_rssi_valid(rssi)
    {
        if (rssi == "-109" || rssi == "-127")
            return false;
        return true;
    }

    function insert_stalist_table(ifname, ssid, sta_list)
    {
        var i, tr_row, td_cell;
        var rssi_str, host_str;
        var tableid = document.getElementById('iw-assoclist');

        for (i = 0; sta_list[i] ; i++){
            //console.log(sta_list[i]);
            tr_row = tableid.insertRow(-1);

            tr_row.align = "center";
            td_cell = tr_row.insertCell(-1);
            td_cell.innerHTML = ifname;

            td_cell = tr_row.insertCell(-1);
            td_cell.innerHTML = ssid;

            td_cell = tr_row.insertCell(-1);
            td_cell.innerHTML = sta_list[i].MacAddr;

            host_str = sta_list[i].hostname
            if (sta_list[i].ipv4 != "")
                host_str = host_str + ", " + sta_list[i].ipv4
            if (sta_list[i].ipv6 != "")
                host_str = host_str + "<br>" + sta_list[i].ipv6
            td_cell = tr_row.insertCell(-1);
            td_cell.innerHTML = host_str;

            td_cell = tr_row.insertCell(-1);
            td_cell.innerHTML = sta_list[i].security;

            rssi_str = sta_list[i].AvgRssi0;
            if (is_rssi_valid(sta_list[i].AvgRssi1))
                rssi_str = rssi_str + ", " + sta_list[i].AvgRssi1;
            if (is_rssi_valid(sta_list[i].AvgRssi2))
                rssi_str = rssi_str + ", " + sta_list[i].AvgRssi2;
            if (is_rssi_valid(sta_list[i].AvgRssi3))
                rssi_str = rssi_str + ", " + sta_list[i].AvgRssi3;
            td_cell = tr_row.insertCell(-1);
            td_cell.innerHTML = rssi_str;

            //TxRate
            td_cell = tr_row.insertCell(-1);
            td_cell.innerHTML = sta_list[i].TxRate + "Mbps," + sta_list[i].Bw+ "MHz" + "<br>" + sta_list[i].PhyMode + ", " + sta_list[i].Mcs + ", " + sta_list[i].Gi;

            //RxRate
            td_cell = tr_row.insertCell(-1);
            td_cell.innerHTML = sta_list[i].RxRate + "Mbps," + sta_list[i].LastBw + "MHz" + "<br>" + sta_list[i].LastPhyMode + ", " + sta_list[i].LastMcs + ", " + sta_list[i].LastGi;

            td_cell = tr_row.insertCell(-1);
            td_cell.innerHTML = sta_list[i].Hr + ":" + sta_list[i].Min + ":" + sta_list[i].Sec;
        }
    }

    function disable_all_buttons()
    {
        var inp = document.getElementsByTagName("BUTTON");
        for(var idx=0; idx < inp.length; idx++){
            inp[idx].disabled = true;
        }
    }

    function enable_all_buttons()
    {
        var inp = document.getElementsByTagName("BUTTON");
        for(var idx=0; idx < inp.length; idx++){
            inp[idx].disabled = false;
        }
    }

    function visit_url(idSuffix,to)
    {
        //disable_all_buttons();
        //document.getElementById("ActionBtns_"+idSuffix).style.display = "none";
        //document.getElementById("BusyText_"+idSuffix).style.display = "";
        location.href = to;
    }

    function visit_url_confirm(msg, idSuffix, to) {
        var i = confirm(msg);
        if (i)
            visit_url(idSuffix,to);
    }

    function add_vif(devName, vifPrefix, maxVif)
    {
        if (!confirm('<%:Are you sure to ADD wireless interface?%>'))
            return;
        disable_all_buttons();
        document.getElementById("ActionBtns_"+devName).style.display = "none";
        document.getElementById("BusyText_"+devName).style.display = "";
        if(isNaN(maxVif) || !/^\d{1,}$/.test(maxVif)){
            alert("Failed to add a new interface!\nERROR: Incorrect maximum supported VIF setting!\nmaxVif = " + maxVif);
            location.reload(true);
            return;
        }
        XHR.get('<%=luci.dispatcher.build_url("admin", "network", "wifi", "get_bssid_num")%>/' + devName, null,
            function(x)
            {
                try{
                    var resp = JSON.parse(x.response);
                    if(resp["status"] == "SUCCESS"){
                        if(isNaN(resp["bssidNum"]) || !/^\d{1,}$/.test(resp["bssidNum"]) || parseInt(resp["bssidNum"]) > parseInt(maxVif)){
                            alert("Failed to add a new interface!\nERROR: Device settings have been corrupted!\nBssidNum in Response is: " + resp["bssidNum"]);
                            location.reload(true);
                        }
                        else if(parseInt(resp["bssidNum"]) == parseInt(maxVif)){
                            alert("As the Device " + devName + " supports maximum " + maxVif + " virtual interfaces, no more virtual interfaces can be added!");
                        }
                        else{
                            location.href = '<%=luci.dispatcher.build_url("admin", "network", "wifi", "vif_add_view")%>/' + devName + '/' + vifPrefix;
                        }
                    }
                    else{
                        alert("Failed to add a new interface!\nERROR: " + resp["status"]);
                    }
                }
                catch(e){
                    alert("Failed to add a new interface!\nERROR: Incorrect response received!");
                    console.log("Failed to add a new interface!\nERROR: [" + e.name + "] " + e.message);
                }
                finally{
                    enable_all_buttons();
                    document.getElementById("ActionBtns_"+devName).style.display = "";
                    document.getElementById("BusyText_"+devName).style.display = "none";
                }
            }
        );
    }
    var stainfo = [];
    var stainfo_inited = 0;
    function staInfoHandler()
    {
        for (var i=0; stainfo[i]; i++) {
            //console.log(stainfo[i].ifname);
            XHR.poll(3, "<%=luci.dispatcher.build_url("admin",  "network",  "wifi",  "sta_info")%>/" + stainfo[i].ifname, null, 
                function(x)
                {
                    try{
                        stainfo_inited = 1;
                        //console.log(x.responseText);
                        var response = JSON.parse(x.responseText);
                        for (var j=0; stainfo[j]; j++) {
                            if (response[0].ifname == stainfo[j].ifname) {
                                response.shift(); //delete first item
                                stainfo[j].sta = response;
                            }
                        }
                    }
                    catch(e){}
                }
            );
        }
    }

    function printstainfo()
    {
        if (stainfo.length == 0) {
            clean_stalist_table();
            return;
        }
        if (stainfo_inited) {
            //console.log(stainfo);
            clean_stalist_table();
            var stabuf = stainfo;
            for (var i=0; stabuf[i]; i++) {
                insert_stalist_table(stabuf[i].ifname, stabuf[i].ssid, stabuf[i].sta);
            }
        }
        setTimeout(printstainfo, 2000);
    }

    function apcliInfoTimerHandler(vifNameList,idx,remSec)
    {
        XHR.get('<%=luci.dispatcher.build_url("admin", "network", "wifi", "get_apcli_conn_info")%>/' + vifNameList[idx], null,
            function(x)
            {
                try{
                    updateApCliInfoCb(vifNameList,idx,remSec,JSON.parse(x.response));
                }
                catch(e){
                    var y={conn_state:"Disconnected"};
                    updateApCliInfoCb(vifNameList,idx,0,y);
                }
            }
        );
    }
    function updateApCliInfoCb(vifNameList,idx,remSec,connInfo)
    {
        var vifName = vifNameList[idx];
        if(connInfo.hasOwnProperty("conn_state") && connInfo["conn_state"] == "Connected"){
            document.getElementById("ConnStatus_"+vifName).innerHTML = "<%:Connected%>";
            document.getElementById("ApCliBssid_"+vifName).innerHTML = (connInfo.hasOwnProperty("bssid")) ? connInfo["bssid"] : "?";
            document.getElementById("ApCliSsid_"+vifName).innerHTML = (connInfo.hasOwnProperty("ssid")) ? connInfo["ssid"].replace(/ /g,"&nbsp;") : "";
            document.getElementById("LoadingBar_"+vifName).style.display = "none";
            document.getElementById("ApCliDownBtn_"+vifName).style.display = "";
            document.getElementById("ApCliDisConnBtn_"+vifName).style.display = "";
            document.getElementById("ApCliCfgBtn_"+vifName).style.display = "";
            document.getElementById("ConnInfo_"+vifName).style.display = "";
            if(idx < vifNameList.length - 1){
                setTimeout(apcliInfoTimerHandler,1000,vifNameList,idx+1,5);
            }
        }
        else{
            if(remSec > 1){
                setTimeout(apcliInfoTimerHandler,1000,vifNameList,idx,remSec - 1);
            }
            else{
                if (connInfo.hasOwnProperty("conn_state") && (connInfo["conn_state"] == "Connected")) {
                    document.getElementById("ConnStatus_"+vifName).innerHTML = "<%:Connected%>";
                } else {
                    document.getElementById("ConnStatus_"+vifName).innerHTML = "<%:Disconnected%>";
                }
                document.getElementById("LoadingBar_"+vifName).style.display = "none";
                document.getElementById("DisConnMsg_"+vifName).style.display = "";
                if (connInfo.hasOwnProperty("infc_state") && connInfo["infc_state"] == "up"){
                    document.getElementById("ApCliDownBtn_"+vifName).style.display = "";
                }
                else{
                    document.getElementById("ApCliUpBtn_"+vifName).style.display = "";
                }
                document.getElementById("ApCliConnBtn_"+vifName).style.display = "";
                document.getElementById("ApCliCfgBtn_"+vifName).style.display = "";
                if(idx < vifNameList.length - 1){
                    setTimeout(apcliInfoTimerHandler,1000,vifNameList,idx+1,5);
                }
            }
        }
    }
    function updateApCliInfo()
    {
        var apCliInfcList=[];
        <% for _,dev in ipairs(devs) do %>
            <% if dev.apcli and dev.ApCliEnable == "1" then %>
                apCliInfcList.push("<%=dev.apcli.devname%>");
            <% end %>
        <% end %>
        if (apCliInfcList.length >= 1 ){
            setTimeout(apcliInfoTimerHandler,1000,apCliInfcList,0,3);
        }
    }

    function get_apply_status_cb(rsp)
    {
        try{
            var r = JSON.parse(rsp);
        }
        catch(e){
            return;
        }
        if(r.status == "ON_PROGRESS"){
            var u = confirm("Device is applying the saved settings now!\n" +
                "It is recommended to wait until all the saved settings are applied.\n" +
                "Please click on 'OK' button to wait for the device.\n" +
                "or click on 'Cancel' button to configure the settings available in current web-page.");
            if (u == true) {
                location = '<%=luci.dispatcher.build_url("admin", "network", "wifi", "loading")%>' + window.location.pathname;
            }
        }
    }

    function get_apply_status()
    {
        XHR.get('<%=luci.dispatcher.build_url("admin", "network", "wifi", "get_apply_status")%>', null,
            function(x)
            {
                console.log(x);
                get_apply_status_cb(x.response);
            }
        );
    }

    window.onload = function(){
        get_apply_status();
        updateApCliInfo();
        <% for _,dev in ipairs(devs) do %>
            <% if dev.vifs then%>
                <% for _,vif in ipairs(dev.vifs) do %>
                    <% if vif.state == "up" then %>
                        stainfo.push({"ifname": "<%=vif.vifname%>", "ssid": "<%=vif.__ssid%>", "sta": ""});
                    <% end %>
                <% end %>
            <% end %>
        <% end %>
        staInfoHandler(stainfo);
        printstainfo();
    }
</script>
    <%+footer%>
